<%@ page import="domain.Product" %>
<%@ page import="java.util.Map" %>
<%@ page import="domain.Order" %>
<%@ page import="org.apache.commons.beanutils.BeanUtils" %>
<%@ page import="java.lang.reflect.InvocationTargetException" %>
<%@ page import="java.util.Date" %>
<%@ page import="domain.OrderItem" %>
<%@ page import="utils.IdUtils" %>
<%@ page import="dao.OrderDao" %>
<%@ page import="utils.MailUtils" %>
<%--
  Created by IntelliJ IDEA.
  User: ThinkPad
  Date: 2021/12/26
  Time: 1:57
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" isELIgnored="false"%>
<html>
<head>
    <title>订单已提交！</title>
</head>
<body>
<%@include file="head.jsp"%>
<%
    Map<Product,Integer> cart = (Map<Product, Integer>) session.getAttribute("cart");
    Order order = new Order();
    // 利用BeanUtils接收并封装自表单传来的各个参数
    try {
        BeanUtils.populate(order,request.getParameterMap());
    } catch (IllegalAccessException | InvocationTargetException e) {
        e.printStackTrace();
    }
    order.setId(IdUtils.getUUID());
    order.setUid(user.getId());
    order.setOrdertime(new Date());
    for (Product p:cart.keySet()){
        OrderItem item = new OrderItem();
        item.setOid(order.getId());
        item.setCount(cart.get(p));
        item.setPid(p.getId());
        order.getItems().add(item);
    }
    OrderDao dao = new OrderDao();
    if (dao.insert(order)){
        // 订单创建完成，可以清空购物车
        cart.clear();
        session.setAttribute("cart",cart);
        MailUtils.sendEmail(order,user);
%>
<br />
<div style="text-align: center;font-weight: bold;font-size: x-large;color: forestgreen">
    异世相遇，尽享美味！<br />
    祝贺您，您的订单已成功创建，您可以稍后在<%=order.getRemail()%>邮箱查收发货信息！
</div>
<a href="${pageContext.request.contextPath}/main.jsp" style="margin-left: 45%;color: blue;font-weight: bold">&lt;返回主页</a>
<%
    }
%>
<%@include file="tail.jsp"%>
</body>
</html>
